package com.redxiii.spike.handlers;

import com.redxiii.spike.api.communication.CommunicationService;
import com.redxiii.spike.api.message.ImMessage;
import com.redxiii.spike.api.message.ImStatusMessage;
import com.redxiii.spike.api.message.Message;
import com.redxiii.spike.api.message.MessageHandler;

/**
 * @author Daniel Filgueiras
 * @since Feb 21, 2013
 */
public abstract class AbstractImHandler implements MessageHandler {

	protected CommunicationService service;
	protected ImMessage lastMessage;
	
	@Override
	public void init(CommunicationService service) {
		this.service = service;
	}
	
	@Override
	public boolean handle(Message message) {
		
		if (!(message instanceof ImMessage))
			return false;
		
		lastMessage = (ImMessage) message;
		
		if (lastMessage instanceof ImStatusMessage) {
			return handle((ImStatusMessage)message);
		}
		return handle(lastMessage);
	}

	protected boolean handle(ImMessage message) {
		return false;
	}
	
	protected boolean handle(ImStatusMessage message) {
		return false;
	}
	
	protected void replay(CharSequence response) {
		service.reply(lastMessage, response);
	}

}
